import { getTotalOfChild, getTreeMaxLevel } from "@/utils/tree";
export default {
  methods: {
    /**
     * 扁平化成层级数组数据
     * @param {Array} treeData 树结构
     */
    flattenToLevelArr(tree) {
      let res = [];
      let maxRowNum = getTreeMaxLevel([tree]); //共有多少层数据

      function traverseTree(node, currentLevel) {
        if (!res[currentLevel]) {
          res[currentLevel] = [];
        }
        // 某节点下有多少个子/孙节点
        const totalOfChild = getTotalOfChild(node, true);

        res[currentLevel].push({
          ...node,
          colspan: totalOfChild || 1,
          rowspan: node.children?.length > 0 ? 1 : maxRowNum - currentLevel,
        });
        if (node.children && node.children.length > 0) {
          for (let i = 0; i < node.children.length; i++) {
            traverseTree(node.children[i], currentLevel + 1); // 遍历子节点并传入更新后的层级值
          }
        }
      }

      traverseTree(tree, 0); // 从根节点开始遍历

      return res;
    },
    resetRowSpan(tableData) {
      const dataColumns = this.dataColumns;
      for (let i = 0; i < tableData.length; i++) {
        const row = tableData[i];
        for (let j = 0; j < dataColumns.length; j++) {
          const col = dataColumns[j];
          if (row[col.prop].rowspan != undefined) {
            delete row[col.prop].rowspan;
          }
        }
      }
    },
    /**
     * 格式化表格数据，单元格上下相同时自动合并单元格
     * @param {*} tableData
     */
    setRowSpanData(tableData, isReset) {
      if (this.isAutoMergeRowspan != true) {
        return;
      }
      if (isReset == true) {
        this.resetRowSpan(tableData);
      }
      for (let i = 0; i < this.dataColumns.length; i++) {
        const prevCol = this.dataColumns[i - 1] || null;
        const col = this.dataColumns[i];
        let start = null;
        for (let j = 0; j < tableData.length; j++) {
          if (j >= tableData.length - 1) {
            continue;
          }
          const row = tableData[j];
          const nextRow = tableData[j + 1];

          if (
            col.mergeCell === true &&
            ![undefined, null, ""].includes[row[col.prop].value] &&
            row[col.prop].value == nextRow[col.prop].value &&
            (prevCol == null || nextRow[prevCol.prop].rowspan == 0)
          ) {
            // 该单元格与下一行内容相同
            if (start == null) {
              start = j;
            }

            if (tableData[start][col.prop].rowspan == undefined) {
              tableData[start][col.prop].rowspan = 1;
            }
            if (tableData[j + 1][col.prop].rowspan == undefined) {
              tableData[j + 1][col.prop].rowspan = 1;
            }

            tableData[start][col.prop].rowspan += 1;
            tableData[j + 1][col.prop].rowspan = 0;
          } else {
            start = null;
          }
        }
      }
    },
    formatterData(tableData) {
      let res = [];
      const { dataColumns } = this;
      for (let i = 0; i < tableData.length; i++) {
        const row = tableData[i];
        let _row = { [this.rowKey]: row[this.rowKey], sourceRow: { ...row } };

        for (let j = 0; j < dataColumns.length; j++) {
          const col = dataColumns[j];
          _row[col.prop] = { value: row[col.prop] };
        }
        res.push(_row);
      }
      return res;
    },
  },
};
